<template>
<div class="nva_menu_container" :class="[{'no-sidebar': userData && userData.roleKey !== 'county'}]">
  <div class="menu_container wst" v-if="userData && userData.roleKey === 'county'">
    <Sidebar ref="cSideBar" name="顺序编排" :siderList="pointlist" @handleAcitve="handleAcitve"></Sidebar>
  </div>
  <div class="container" :class="[{'block-container': userData && userData.roleKey !== 'county'}]">
    <p class="title">{{userData && userData.roleKey === 'county' ? '编排学校' : '考场编排管理'}}</p>
    <div class="operation">
      <el-button type="primary" class="c-btn" @click="createManually('new')" v-if="userData && userData.roleKey === 'county'">新建编排</el-button>
      <el-button type="primary" class="c-btn" @click="createManually('reset')" v-if="userData && userData.roleKey === 'county'">重新编排</el-button>
      <el-button type="primary" class="c-btn" @click="handleDeleteManually()" v-if="userData && userData.roleKey === 'county'">删除编排</el-button>
      <el-button type="primary" class="c-btn" style="position: relative;" v-if="userData && userData.roleKey === 'county'">
        <a :href='GLOBAL.COMMON + "/organize/export/organizationData/byPointId?pointId="+ schoolId' class="export-btn"></a>
          打印编排表
      </el-button>

      <el-button type="primary" class="c-btn" style="position: relative;" v-if="userData && userData.roleKey === 'school'">
        <a :href='GLOBAL.COMMON + "//organize/export/organizationData/bySchoolAdminArea"' class="export-btn"></a>
          打印编排表
      </el-button>
      <el-button type="primary" class="c-btn" @click="submitOrganize" v-if="userData && userData.roleKey === 'county'">上报编排</el-button>
    </div>

    <!-- 编排表格 -->
    <div class="room-wrap">
      <div class="room-item" v-for="(item, idx) in manuallyList" :key="idx">
        <div class="room-title clearfix">
          <span>考点：{{ schoolName }}</span>
          <span>考试日期：{{ item.date }}</span>
          <span>总计：{{ item.examineeInfo }}</span>
          <div class="opt pull-right">
            <span @click="item.expand = !item.expand">
              <a>{{ item.expand ? '收起' : '展开' }}</a>
            </span>
          </div>
        </div>
        <div class="table-wrapper" v-show="item.expand">
          <el-table :data="item.manuallyData" :span-method="objectSpanMethod" border class="c-table" style="width: 100%" v-loading="loading">
            <el-table-column prop="batch" label="批次" width="70"></el-table-column>
            <el-table-column prop="waitTime" label="候考时间" width="110"></el-table-column>
            <el-table-column prop="examTime" label="考试时间" width="110"></el-table-column>
            <el-table-column prop="roomName" label="考场号"></el-table-column>
            <el-table-column prop="admissionRange" label="准考证号区间"></el-table-column>
            <el-table-column prop="schoolsName" label="学校"></el-table-column>
            <el-table-column prop="examineeNum" label="人数" width="80"></el-table-column>
            <el-table-column prop="staffInvigilate" label="监考老师" width="120"></el-table-column>
          </el-table>
        </div>
      </div>
    </div>

    <!-- 删除确认 -->
    <kw-delconfim :delVisible.sync="delDialog" delCont="您确定需要删除编排信息吗？" @handleDelete="handleDelete"></kw-delconfim>
    <kw-delconfim :delVisible.sync="resetDialog" title="重新编排提示" delCont="您确定需要重新编排信息吗？重新编排将会删除之前的编排信息。" @handleDelete="handleReset"></kw-delconfim>
  </div>
</div>
</template>

<script>
import kwDelconfim from '@/components/DelConfim.vue'
import Sidebar from '@/components/ContSidebar.vue'
import { mapState } from 'vuex'
export default {
  components: {
    Sidebar,
    kwDelconfim
  },
  data () {
    return {
      areaId: null,
      schoolName: null,
      schoolId: null,
      delDialog: false,
      resetDialog: false,
      loading: false,
      manuallyList: [],
      pointlist: [],
      userData: null
    }
  },
  computed: mapState({
    changeNotify: state => state.changeNotify
  }),
  created () {
    this.userData = JSON.parse(sessionStorage.getItem('userData'))
    this.getPoints()
  },
  methods: {
    // 获取考点列表
    getPoints () {
      let that = this
      that.$http.getPoint().then(res => {
        if (res.code === 20000 && res.data.length) {
          that.pointList = res.data.map(item => {
            let _item = {}
            _item.id = item.id
            _item.name = item.areaName
            return _item
          })
          that.schoolName = that.pointList[0].name || 0
          that.schoolId = that.pointList[0].id || 0
          if (that.userData && that.userData.roleKey === 'county') {
            that.$refs.cSideBar.getPointList(that.pointList)
          }
          this.getManually()
        } else {
          that.$message.error('暂无考点信息, 请先创建考点和考场再进行编排')
        }
      })
    },
    handleAcitve (item) {
      this.schoolName = item.name
      this.schoolId = item.id
      this.getManually()
    },
    handleDeleteManually () {
      this.delDialog = true
    },
    // 删除回调 删除编排
    handleDelete () {
      let that = this
      that.$http.delManuallyByPointId({ pointId: that.schoolId }).then(res => {
        if (res.code === 20000) {
          that.$message.success('删除编排成功')
          this.getManually()
        } else {
          that.$message.error(res.msg)
        }
      })
    },
    // 重新编排 先删除再编排
    handleReset () {
      let that = this
      that.$http.delManuallyByPointId({ pointId: that.schoolId }).then(res => {
        if (res.code === 20000) {
          this.$router.push({ name: 'sequenceSchoolStep1' })
        } else {
          that.$message.error(res.msg)
        }
      })
    },
    // 人工编排
    createManually (type) {
      if (type === 'new') {
        this.$router.push({ name: 'sequenceSchoolStep1' })
      } else {
        this.resetDialog = true
      }
    },
    // 获取编排表
    getManually () {
      let that = this
      that.loading = true
      if (that.userData && that.userData.roleKey === 'county') {
        // 区级编排信息
        that.$http.getManuallyByPonitAreaId({ pointAreaId: that.schoolId }).then(res => {
          if (res.data.length) {
            that.manuallyList = res.data.map(item => {
              let _item = item
              _item.expand = true
              _item.loading = false
              _item.manuallyData = []
              _item.batchData.map((batch, bidx) => {
                batch.roomData.map((room, rIdx) => {
                  let _roomItem = {}
                  _roomItem.batch = batch.batch
                  _roomItem.rowspan = !rIdx ? batch.roomData.length : 0
                  _roomItem.examTime = batch.examTime
                  _roomItem.waitTime = batch.waitTime
                  _roomItem.admissionRange = room.admissionRange
                  _roomItem.examineeNum = room.examineeNum
                  _roomItem.roomCode = room.roomCode
                  _roomItem.roomName = room.roomName
                  _roomItem.schoolsName = room.schoolsName
                  _roomItem.staffInvigilate = room.staffInvigilate
                  _item.manuallyData.push(_roomItem)
                })
              })
              return _item
            })
          }
          that.loading = false
        })
      } else {
        // 校级编排信息
        that.$http.getSchoolAdminByArea().then(res => {
          if (res.data.length) {
            that.manuallyList = res.data.map(item => {
              let _item = item
              _item.expand = true
              _item.loading = false
              _item.manuallyData = []
              _item.batchData.map((batch, bidx) => {
                batch.roomData.map((room, rIdx) => {
                  let _roomItem = {}
                  _roomItem.batch = batch.batch
                  _roomItem.rowspan = !rIdx ? batch.roomData.length : 0
                  _roomItem.examTime = batch.examTime
                  _roomItem.waitTime = batch.waitTime
                  _roomItem.admissionRange = room.admissionRange
                  _roomItem.examineeNum = room.examineeNum
                  _roomItem.roomCode = room.roomCode
                  _roomItem.roomName = room.roomName
                  _roomItem.schoolsName = room.schoolsName
                  _roomItem.staffInvigilate = room.staffInvigilate
                  _item.manuallyData.push(_roomItem)
                })
              })
              return _item
            })
          }
          that.loading = false
        })
      }
    },

    objectSpanMethod ({ row, column, rowIndex, columnIndex }) {
      if (columnIndex === 0 || columnIndex === 1 || columnIndex === 2) {
        if (row.rowspan) {
          return [row.rowspan, 1]
        } else {
          return [0, 0]
        }
      }
    },
    // 提交上报审核
    submitOrganize () {
      this.$http.getOrganize().then(res => {
        if (res.code === 20000) {
          this.$message.success('上报编排成功！')
          this.$store.commit('CHANGE_NOTIFY', !this.changeNotify)
        } else {
          this.$message.error(res.msg)
        }
      })
    }
  }
}
</script>

<style lang="scss" scoped>
.table-wrapper{
  margin-top: 0;
}
.room-item {
  margin-bottom: 20px;
  margin-top: 20px;
  .room-title{
    width: 100%;
    height: 50px;
    background-color: #3D68D5;
    color: #fff;
    box-sizing: border-box;
    padding: 0 30px;
    line-height: 50px;
    span{
      margin-right: 30px;
    }
    .pull-right{
      span{
        margin-right: 0;
      }
    }
    a{
      color: #fff;
      cursor: pointer;
      i{
        margin-right: 6px;
      }
    }
  }
}
.export-btn{
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  margin-right: 0;
  opacity: 0;
}
</style>

<style>
.container {
  margin-left: 190px;
  width: calc(100% - 240px);
}
</style>
